package com.collegeproject.weatherapp.fragments;

import java.util.HashMap;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.collegeproject.weatherapp.R;
import com.collegeproject.weatherapp.StartActivity.AppSectionsPagerAdapter;
import com.collegeproject.weatherapp.StartActivity.FragmentListener;
import com.collegeproject.weatherapp.library.DatabaseHandler;

public class DashboardFragment extends Fragment {
	Button btnLogout, btnPost, btnDetails, btnDelete;
	TextView username;
	static FragmentListener _listener;

	public static final String TAG = "DashboardFragment";

	public static DashboardFragment newInstance(FragmentListener listener) {
		DashboardFragment f = new DashboardFragment();
		_listener = listener;
		return f;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.dashboard, container, false);
	}

	@Override
	public void onStart() {
		if (isUserLoggedIn(getActivity())) {
			// user already logged in show dash board

			btnLogout = (Button) getView().findViewById(R.id.btnLogout);
			btnPost = (Button) getView().findViewById(R.id.btnAddPost);
			btnDetails = (Button) getView().findViewById(R.id.btnEditDetails);
			btnDelete = (Button) getView().findViewById(R.id.btnDeleteUser);
			btnDelete.setEnabled(false);
			btnDelete.setVisibility(View.GONE);
			username = (TextView) getView().findViewById(R.id.textViewUsername);

			setUserInfo(getActivity());
			btnLogout.setOnClickListener(new View.OnClickListener() {

				public void onClick(View arg0) {
					logoutUser(getActivity());

					// switch to login fragment
					AppSectionsPagerAdapter.FragmentToChangeTo = 1;
					_listener.onSwitchToNextFragment();
				}
			});

			btnPost.setOnClickListener(new View.OnClickListener() {
				public void onClick(View arg0) {

					// switch to post fragment
					AppSectionsPagerAdapter.FragmentToChangeTo = 2;
					_listener.onSwitchToNextFragment();
				}
			});

			btnDetails.setOnClickListener(new View.OnClickListener() {
				public void onClick(View arg0) {

					// switch to edit fragment
					AppSectionsPagerAdapter.FragmentToChangeTo = 3;
					_listener.onSwitchToNextFragment();
				}
			});

			btnDelete.setOnClickListener(new View.OnClickListener() {
				public void onClick(View arg0) {

					// switch to users fragment
					AppSectionsPagerAdapter.FragmentToChangeTo = 4;
					_listener.onSwitchToNextFragment();
				}
			});

		} else {

			// user is not logged in show login screen, switch to login fragment
			AppSectionsPagerAdapter.FragmentToChangeTo = 1;
			_listener.onSwitchToNextFragment();
		}
		super.onStart();
	}

	public boolean isUserLoggedIn(Context context) {
		DatabaseHandler db = new DatabaseHandler(context);
		int count = db.getRowCount();
		if (count > 0) {
			// user logged in
			return true;
		}
		return false;
	}

	public void setUserInfo(Context context) {
		DatabaseHandler db = new DatabaseHandler(context);

		HashMap<String, String> user = new HashMap<String, String>(
				db.getUserDetails());

		username.setText("Welcome " + user.get("firstname") + " "
				+ user.get("lastname"));

		if (user.get("username").equals("admin")) {
			btnDelete.setEnabled(true);
			btnDelete.setVisibility(View.VISIBLE);
		}
	}

	public void logoutUser(Context context) {
		DatabaseHandler db = new DatabaseHandler(context);
		db.resetTables();
	}

}
